package cn.kongwg.yygh.user.api;

import cn.kongwg.yygh.common.result.Result;
import cn.kongwg.yygh.common.utils.AuthContextHolder;
import cn.kongwg.yygh.model.user.UserInfo;
import cn.kongwg.yygh.user.service.UserInfoService;
import cn.kongwg.yygh.vo.user.LoginVo;
import cn.kongwg.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author kongwg
 * @create 2021-04-15 17:24
 */
@RestController
@RequestMapping("/api/user")
public class UserApiController {


    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> map = userInfoService.login(loginVo);
        return Result.ok(map);

    }

    @ApiOperation("用户认证")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {

        userInfoService.userAuth(AuthContextHolder.getUserInfoId(request), userAuthVo);

        return Result.ok();
    }


    //获取用户id信息接口
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userInfoId = AuthContextHolder.getUserInfoId(request);
        UserInfo userInfo = userInfoService.getById(userInfoId);
        return Result.ok(userInfo);
    }
}
